package com.subang.framework.web.service;


import com.subang.common.core.domain.AjaxResult;
import com.subang.common.core.domain.entity.SysUser;
import com.subang.common.utils.SecurityUtils;
import com.subang.system.domain.SysUserPointsDetail;
import com.subang.system.service.ISysUserPointsDetailService;
import com.subang.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserIntegralService {

    @Autowired
    private ISysUserPointsDetailService sysUserPointsDetailService;

    @Autowired
    private ISysUserService userService;

    public AjaxResult updateUserIntegral(String action) {

        SysUser user = userService.getUserByOpenId(SecurityUtils.getLoginUser().getUser().getOpenid());
        long integralAmount = getIntegralAmount(action);

        // 当积分变动为负数时，检查用户积分是否充足
        if (integralAmount < 0) {
            long requiredPoints = -integralAmount; // 转换为正数表示需要消耗的积分
            if (user.getTotalPoints() < requiredPoints) {
                return AjaxResult.error("用户积分不足，无法完成操作，请努力赚积分！");
            }
        }


        // 更新用户积分
        SysUserPointsDetail sysUserPointsDetail = new SysUserPointsDetail();
        sysUserPointsDetail.setPointsChange(integralAmount);
        sysUserPointsDetail.setChangeType(translateAction(action));
        sysUserPointsDetail.setChangeTime(new Date());
        sysUserPointsDetail.setUserId(user.getUserId());
        sysUserPointsDetailService.insertSysUserPointsDetail(sysUserPointsDetail);

        user.setTotalPoints(user.getTotalPoints() + integralAmount);
        userService.updateUser(user);

        return AjaxResult.success("积分更新成功");
    }

    private long getIntegralAmount(String action) {
        switch (action) {
            case "firstLogin":
                return 10;
            case "dailyLogin":
                return 1;
            case "dailyMark":
                return 2;
            case "mockTest":
                return 5;
            case "dailyShare":
                return 2; // 统一新增积分
            case "table-analysis":
                return 10; // 统一新增积分
            case "basic-calculations":
                return 1; // 统一新增积分
            case "evaluateTeacher":
                return -4; // 统一新增积分
            case "viewArticle":
                return -2; // 消耗积分
            case "downloadArticle":
                return -10; // 消耗积分
            case "viewExam":
                return -2; // 消耗积分
            case "findJob":
                return -10; // 消耗积分
            default:
                return 0;
        }
    }

    private String translateAction(String action) {
        switch (action) {
            case "firstLogin":
                return "首次登录";
            case "dailyLogin":
                return "每日登录";
            case "dailyMark":
                return "每日打卡";
            case "mockTest":
                return "行测模考";
            case "dailyShare":
                return "每日分享";
            case "evaluateTeacher":
                return "评价讲师";
            case "viewArticle":
                return "查看文章";
            case "downloadArticle":
                return "下载文章";
            case "table-analysis":
                return "一表通算"; // 统一新增积分
            case "basic-calculations":
                return "基础训练"; // 统一新增积分
            case "viewExam":
                return "查看模考";  // 消耗积分
            case "findJob":
                return "岗位筛选";  // 消耗积分
            default:
                return action; // 如果没有匹配的动作，返回原始动作
        }
    }
}